package app.pages;

import java.io.File;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.stereotype.Component;
import org.vaadin.navigator7.NavigableApplication;
import org.vaadin.navigator7.Page;

import app.downloadupload.DownloadResource;
import app.model.Role;
import app.service.ApplicationService;
import app.service.MemberService;
import app.webapplication.MyApplication;

import com.vaadin.Application;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

@Page
@Component
@Configurable(preConstruction = true)
public class HomePage extends HorizontalLayout {

	@Autowired MemberService memberService;
	@Autowired ApplicationService applicationService;
	
	public HomePage() {
		removeAllComponents();
		this.setSpacing(true);
				
		// Define the 3 columns of the website
		VerticalLayout leftWebsitePart = new VerticalLayout();
		VerticalLayout centerWebsitePart = new VerticalLayout();
		VerticalLayout rightWebsitePart = new VerticalLayout();

		// Setting the width of each part of the website
		centerWebsitePart.setWidth("660px");
		
		// Adding components to the main Window
		addComponent(leftWebsitePart);
		addComponent(centerWebsitePart);
		addComponent(rightWebsitePart);
		centerWebsitePart.setSpacing(true);
		
		// Saying to left an right parts to take all the space still available
		setExpandRatio(leftWebsitePart, 100);
		setExpandRatio(rightWebsitePart, 100);
		
		
		/////////////////////////////////////////////////////////////////
		/////////////////// Displaying Top Apps /////////////////////////
		/////////////////////////////////////////////////////////////////
		
		HorizontalLayout containerLayout = new HorizontalLayout();
		VerticalLayout vContainerLayoutLeft = new VerticalLayout();
		VerticalLayout vContainerLayoutRight = new VerticalLayout();
		
		vContainerLayoutLeft.setSpacing(true);
		vContainerLayoutRight.setSpacing(true); 
		containerLayout.setSpacing(true);		
		
		// Display 10 Apps 
		// TODO : add query here to improve app
		// TODO : extract this code into a method
		for(int i=0;i<10;i++){
					
			// Button which will download an app
			// TODO : the button must select the specified application
			Button topAppButton = new Button("Donwload this application", new ClickListener(){
				@Override public void buttonClick(ClickEvent event) {
					
					Application myApp = (MyApplication)NavigableApplication.getCurrent();

					File file = new File("eclipse.exe");
					System.out.println(file.getAbsolutePath());

					try {
						DownloadResource downloadResource = new DownloadResource(file, myApp);
						myApp.getMainWindow().open(downloadResource, "_new");
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			});
			
			topAppButton.setWidth("325px");
			topAppButton.setHeight("25px");
			
			// 5 first apps in the left part, others in the right part
			if(i<5){
				vContainerLayoutLeft.addComponent(topAppButton);
			} else {
				vContainerLayoutRight.addComponent(topAppButton);
			}
		}
		
		Button becomeDevButton = new Button("Become a mobile developer");
		Button loginButton = new Button("login");
		loginButton.setWidth("655px");
		loginButton.setHeight("25px");
		becomeDevButton.setWidth("655px");
		becomeDevButton.setHeight("25px");
		
		becomeDevButton.addListener(new ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
				
				Window regWindow = new RegisterWindow(Role.DEVELOPER);
				getApplication().getMainWindow().addWindow(regWindow);
			}
		});
		
		loginButton.addListener(new ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
				
				Window loginWindow = new LoginWindow();
				getApplication().getMainWindow().addWindow(loginWindow);
			}
		});
		
		// Adding components and setting alignment
		containerLayout.addComponent(vContainerLayoutLeft);
		containerLayout.addComponent(vContainerLayoutRight);
		centerWebsitePart.addComponent(containerLayout);
		centerWebsitePart.addComponent(becomeDevButton);	
		centerWebsitePart.addComponent(loginButton);
	}
}